package com.hcx.tool;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hcx.Service.PhonePackageService;
import com.hcx.domain.PhonePackage;
import jakarta.annotation.PostConstruct;
import org.apache.tomcat.util.digester.Rule;
import org.springframework.ai.document.Document;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MyTool {
    @Autowired
    private PhonePackageService phonePackageService;

    @Autowired
    private VectorStore vectorStore;

    //查询费用预算
    @Tool(description = "根据条件查询费用预算")
    public List<PhonePackage> getbudget(@ToolParam(required = false, description = "查询条件") RequirementQuery query) {
        QueryWrapper<PhonePackage> qw = new QueryWrapper<>();

        // 处理月租条件
        if (query.getBudget() != null && query.getBudget().length() > 0) {
            switch (query.getBudget()) {
                case "50元以下":
                    qw.le("monthly_rent", 50);
                    break;
                case "50-100元":
                    qw.between("monthly_rent", 50, 100);
                    break;
                case "100元以上":
                    qw.gt("monthly_rent", 100);
                    break;
                default:
                    break;
            }
        }

        // 处理流量条件
        if (query.getTraffic() != null && query.getTraffic().length() > 0) {
            switch (query.getTraffic()) {
                case "轻度文字":
                    qw.like("traffic", "10GB");
                    break;
                case "视频刷剧":
                    qw.ge("traffic", "30GB");
                    break;
                case "直播":
                    qw.ge("traffic", "不限量");
                    break;
                default:
                    break;
            }
        }

        // 处理通话时长条件
        if (query.getCall() != null && query.getCall().length() > 0) {
            if (query.getCall().contains("国内")) {
                try {
                    // 提取通话时长数字（假设格式为"XX分钟"）
                    String callMinutesStr = query.getCall().replaceAll("[^0-9]", "");
                    int callMinutes = Integer.parseInt(callMinutesStr);
                    qw.ge("call_duration", callMinutes);
                } catch (NumberFormatException e) {
                    // 如果解析失败，忽略该条件
                }
            } else {
                qw.like("packageName", "全球通尊享");
            }
        }

        // 处理附加服务条件
        if (query.getAdditional() != null && query.getAdditional().length() > 0) {
            // 副卡查询
            if (query.getAdditional().contains("副卡")) {
                qw.like("additional_service", "副卡");
            }

            // 宽带绑定查询
            if (query.getAdditional().contains("宽带")) {
                qw.like("additional_service", "宽带");
            }

            // 合约期查询
            if (query.getAdditional().contains("6个月")) {
                qw.eq("contract_period", "6个月");
            } else if (query.getAdditional().contains("12个月")) {
                qw.eq("contract_period", "12个月");
            } else if (query.getAdditional().contains("24个月")) {
                qw.eq("contract_period", "24个月");
            }
        }
        return phonePackageService.list(qw);
    }
    @PostConstruct
    private void init() {
        String jsonStr = """
                [
                  {
                    "id": "rule_01",
                    "type": "合约期",
                    "condition": "合约期未满12个月",
                    "description": "提前更换套餐需支付剩余费用的10%作为违约金。"
                  },
                  {
                    "id": "rule_02",
                    "type": "携号转网",
                    "condition": "申请携号转网",
                    "description": "需满足：1. 无欠费 2. 当前套餐已到期 3. 非吉祥号段（如138/139开头）。"
                  }
                ]
                """;
        // 将套餐规则存储到内存向量库中
        List<Document> documents = new ArrayList<>();
        // 解析JSON并创建Document对象
        try {
            // 使用Jackson ObjectMapper解析JSON数组
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonArray = objectMapper.readTree(jsonStr);

            for (JsonNode jsonNode : jsonArray) {
                String description = jsonNode.get("description").asText();
                Document document = new Document(description);
                documents.add(document);
            }
        } catch (Exception e) {
            // 处理JSON解析异常
            e.printStackTrace();
        }
        // 将文档添加到向量存储
        vectorStore.add(documents);

    }
}
